package org.robot.scm.pojos.platform;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.SneakyThrows;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.robot.scm.pojos.BaseNotify;
import org.robot.scm.pojos.TargetNotify;
import org.robot.scm.pojos.config.BaseConfig;
import org.robot.scm.pojos.enums.ChannelEnum;
import org.robot.scm.pojos.enums.HttpMethodEnum;
import org.robot.scm.utils.DateTool;

@Data
@Builder
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class OttiBaseNotify extends BaseNotify {

    private String mac;
    private Double glucose;
    private Long receiveTime;

    /**
     * 异常定义,  非正常区间3.9 - 10mmol/L
     */
    public boolean isAbnormal(BaseConfig config) {
        List<Double> bloodLevel = config.getCurrentPlatform().getNormalBloodLevel();
        // 默认3.9-10
        if (CollectionUtils.size(bloodLevel) != 2) {
            return glucose < 3.9 || glucose > 10;
        }
        return glucose < bloodLevel.get(0) || glucose > bloodLevel.get(1);
    }

    /**
     * 低血糖
     */
    public boolean isLowGlucose(BaseConfig config) {
        List<Double> bloodLevel = config.getCurrentPlatform().getNormalBloodLevel();
        if (CollectionUtils.size(bloodLevel) != 2) {
            return false;
        }
        return glucose < bloodLevel.get(0);
    }

    @SneakyThrows
    @Override
    public TargetNotify barkToNotifyBean(String deviceSn, BaseConfig config) {
        return TargetNotify.builder()
                .channelEnum(ChannelEnum.BARK)
                .notifyUrl(config.get().getNotifyChannel().getBark())
                .deviceSn(deviceSn)
                .topic(config.getCurrentPlatform().getNoticeTitle())
                .content(content(config))
                .detailUrl("")
                .icon(config.getCurrentPlatform().getIconUrl())
                .build();
    }

    @SneakyThrows
    @Override
    public TargetNotify robotToNotifyBean(String robotId, BaseConfig config) {
        String content = config.robotText(robotId, URLEncoder.encode(content(config), StandardCharsets.UTF_8));
        return TargetNotify.builder()
                .httpMethodEnum(HttpMethodEnum.POST)
                .channelEnum(ChannelEnum.ROBOT)
                .notifyUrl(config.robotUrl())
                .deviceSn(StringUtils.EMPTY)
                .topic(config.get().getNotifyChannel().getRobot().getBodyText())
                .content(content)
                .icon(config.getCurrentPlatform().getIconUrl())
                .build();
    }

    @Override
    public boolean equal(BaseNotify another) {
        return Objects.equals(receiveTime, ((OttiBaseNotify) another).getReceiveTime());
    }

    private String content(BaseConfig config) {
        return "【设备】" + config.getCurrentPlatform().getNoticeTitle() + "\n" +
                "【阈值】" + glucose + " mmol/L\n" +
                "【时间】" + DateTool.format(receiveTime, DateTool.PATTERN_7);
    }

}
